#include<windows.h>
#include<iostream>
#include<psapi.h>


using namespace std;

void get_simple_name(char*d,char*s){
	if(s){
		int i=0;
		while(s[i++] != 0);
		int j = 0;
		while(s[--i] != '\\');
		while(d[j++]=s[++i]);
	}
}

void process_name(DWORD processId){
	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
									FALSE,processId);
	
	if(hProcess){
		TCHAR szProcessName[MAX_PATH];
		if(GetProcessImageFileName(hProcess,szProcessName,sizeof(szProcessName))){
			//cout << szProcessName << endl;
			//TCHAR * pFileName = _tcsrchr(szProcessName,'\\');
			//if(pFileName){
				//cout << "process id: " << processId << " ,name: " << pFileName+1 << endl;
			//}
			char d[MAX_PATH];
			get_simple_name(d,szProcessName);
			cout << "process id: " << processId << " , process name: " << d << endl;
			
		}
		CloseHandle(hProcess);
	}
	
	
	
}

int main(){
	DWORD aProcesses[1024],cbNeeded,cProcesses;
	if(EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded)){
		cProcesses = cbNeeded/sizeof(DWORD);
		cout << "total processes number: " << cProcesses << endl; 
		for(int i=0;i<cProcesses;i++){
			if(aProcesses[i] != 0){
				process_name(aProcesses[i]);
			}
		}
	}



	return 0;
}